﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using System.Collections.ObjectModel;
using ThreeByte.ActiveDeck.Config;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Content;
using log4net;
using System.Windows.Threading;

namespace ThreeByte.ActiveDeck.AddIn.Controls.Wizard.Pages
{
    /// <summary>
    /// Interaction logic for ReviewPage.xaml
    /// </summary>
    public partial class ReviewPage : Page, INotifyPropertyChanged
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ReviewPage));
        private readonly CoreConfig Config;
        private Collection<PowerPoint.Slide> Slides;
        private int CurrentSlideIndex;

        public ReviewPage() {
            InitializeComponent();

            Config = WizardMainFrame.ActiveConfig;

            //Construct thumbnails from all of the presentation slides
            Slides = new Collection<PowerPoint.Slide>();

            foreach(PowerPoint.Slide s in Config.Presentation.Slides) {
                Slides.Add(s);
            }


            if(Slides.Count > 0) {
                CurrentSlideIndex = 0;
                DisplaySlide(Slides[CurrentSlideIndex]);
            }
            UpdateButtons();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public PowerPoint.Slide _currentPreviewSlide;
        public PowerPoint.Slide CurrentPreviewSlide {
            get {
                return _currentPreviewSlide;
            }
            set {
                _currentPreviewSlide = value;
                NotifyPropertyChanged("CurrentPreviewSlide");
            }
        }

        private void DisplaySlide(PowerPoint.Slide slide) {
            try {

                SlidePreview.Source = SlideUtil.GetPreview(slide);
                CurrentPreviewSlide = slide;

                DisplayiPadForSlide(slide);

            } catch(Exception ex) {
                log.Warn("Cannot export slide", ex);
            }
        }

        private void DisplayiPadForSlide(PowerPoint.Slide slide) {
            //Look up the slide in the core config and display any preview that exists
            if(Config.ContainsId(slide.SlideID)) {
                SlideConfig sConfig = Config[slide.SlideID];

                //Grab the first PageConfig and load it up
                PageConfig firstPage = sConfig.Pages.First();

                PreviewRoot.Content = PagePreviewFactory.Create(firstPage);

            } else {
                //Show blank iPad
                PreviewRoot.Content = null;
            }

        }


        private void PrevSlideButton_Click(object sender, RoutedEventArgs e) {
            if(CurrentSlideIndex > 0) {
                CurrentSlideIndex = CurrentSlideIndex - 1;
                DisplaySlide(Slides[CurrentSlideIndex]);
            }
            UpdateButtons();
        }

        private void NextSlideButton_Click(object sender, RoutedEventArgs e) {
            if(CurrentSlideIndex < Slides.Count - 1) {
                CurrentSlideIndex = CurrentSlideIndex + 1;
                DisplaySlide(Slides[CurrentSlideIndex]);
            }
            UpdateButtons();
        }

        private void UpdateButtons() {
            NextSlideButton.IsEnabled = (CurrentSlideIndex < Slides.Count - 1);
            PlaySlideButton.IsEnabled = NextSlideButton.IsEnabled;
            PrevSlideButton.IsEnabled = (CurrentSlideIndex > 0);
        }


        private void CannotExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = false;
            e.Handled = true;
        }

        private void HelpExecuted(object sender, ExecutedRoutedEventArgs e) {

        }

        private void Edit_Click(object sender, RoutedEventArgs e) {

        }

        private void PauseSlideButton_Click(object sender, RoutedEventArgs e) {
            lock(_timerLock) {
                _slideshowTimer.Stop();
            }
            PlaySlideButton.Visibility = Visibility.Visible;
            PauseSlideButton.Visibility = Visibility.Collapsed;
        }

        private DispatcherTimer _slideshowTimer;
        private object _timerLock = new object();

        private void PlaySlideButton_Click(object sender, RoutedEventArgs e) {
            //Start the slideshow timer
            lock(_timerLock){
                if(_slideshowTimer == null){
                    _slideshowTimer = new DispatcherTimer();
                    _slideshowTimer.Tick += new EventHandler(_slideshowTimer_Tick);
                    _slideshowTimer.Interval = TimeSpan.FromSeconds(3);
                }
                _slideshowTimer.Start();
            }
            PlaySlideButton.Visibility = Visibility.Collapsed;
            PauseSlideButton.Visibility = Visibility.Visible;
        }

        void _slideshowTimer_Tick(object sender, EventArgs e) {
            
            //Go next
            try {
                if(CurrentSlideIndex < Slides.Count - 1) {
                    CurrentSlideIndex = CurrentSlideIndex + 1;
                    DisplaySlide(Slides[CurrentSlideIndex]);
                    UpdateButtons();

                } else {
                    //Disable the timer because no more slides left
                    lock(_timerLock) {
                        _slideshowTimer.Stop();
                        PlaySlideButton.Visibility = Visibility.Visible;
                        PauseSlideButton.Visibility = Visibility.Collapsed;
                    }
                }

            } catch(Exception ex) {
                log.Error("Error changing slides", ex);
            }
        }



       
    }
}
